#include "Zigbee3InfraredXml.h"


Zigbee3InfraredXml::Zigbee3InfraredXml()
{
    InfraredID.clear();
    InfraredID.append(0xAA);
    InfraredID.append(0XAA);
}

bool Zigbee3InfraredXml::parser()
{
    InfraredList.clear();
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "VensiInfrared")   //解析Device
        {
            QDomNode InfraredChild = rootChild.toElement().firstChild();
            Zigbee3InfraredStruct InfraredStructData;
            while(!InfraredChild.isNull())
            {
                if (InfraredChild.toElement().tagName() == "NWK")         //获取Zigbee的MAC地址
                {
                    QString nwk = InfraredChild.toElement().text().remove(" ");
                    InfraredStructData.m_nwk=QByteArray::fromHex(nwk.toLatin1());

                }
                else if (InfraredChild.toElement().tagName() == "Value")    //获取Zigbee的NWK地址
                {
                    QString Valuedata = InfraredChild.toElement().text().remove(" ");
                    InfraredStructData.m_PointValue=Valuedata.toUInt();
                }
                else if(InfraredChild.toElement().tagName() == "Data")
                {
                    QString InfraredData = InfraredChild.toElement().text().remove(" ");
                    InfraredStructData.m_data=QByteArray::fromHex(InfraredData.toLatin1());
                }
                InfraredChild = InfraredChild.nextSibling();
            }
            InfraredList.append(InfraredStructData);

        }
        rootChild=rootChild.nextSibling();
    }
    return true;
}

bool Zigbee3InfraredXml::modify(const quint16 &number, const Zigbee3InfraredStruct &InfraredData)
{
    if(!this->check())
    {
        return false;
    }

    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root=doc.documentElement();
    QDomNode rootChild = root.firstChild();
    Zigbee3InfraredStruct checkData=InfraredDataMap.value(number);
    if(checkData.m_nwk==InfraredData.m_nwk)
    {
        InfraredDataMap.remove(number);
    }

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "VensiInfrared")   //解析Device
        {
            QDomNodeList nodeList = rootChild.childNodes();
            for(int i=0; i<nodeList.size(); i++)
            {
                QDomNode node = nodeList.at(i);
                if(node.toElement().tagName() == "NWK")
                {
                    QByteArray nwk = QByteArray::fromHex(node.toElement().text().remove(" ").toLatin1());
                    if(nwk == InfraredData.m_nwk)
                    {
                        for(int i=0; i<nodeList.size(); i++)
                        {
                            QDomNode node = nodeList.at(i);
                            if(node.toElement().tagName() == "Value")
                            {
                                QString Valuedata = node.toElement().text().remove(" ");
                                if(Valuedata.toUInt() == InfraredData.m_PointValue)
                                {
                                    qDebug()<<"remove more childdata";
                                   root.removeChild(rootChild);
                                }
                            }
                        }
                    }
                }
            }
        }
        rootChild=rootChild.nextSibling();
    }


    QDomElement zigbeeElement = doc.createElement("VensiInfrared");

    QDomText text;
    QDomElement nwkElement = doc.createElement("NWK");
    QString nwk = InfraredData.m_nwk.toHex().toUpper();
    int length = nwk.length()/2;
    for(int i=1; i<length; i++)
    {
        nwk.insert(2*i+i-1," ");
    }
    text = doc.createTextNode(nwk);
    nwkElement.appendChild(text);
    zigbeeElement.appendChild(nwkElement);

    quint16 DataValue=InfraredData.m_PointValue;

    QDomText Valuetext;
    QDomElement ValueElement = doc.createElement("Value");
    QString value = QString::number(DataValue,10).toUpper() ;

    Valuetext = doc.createTextNode(value);
    ValueElement.appendChild(Valuetext);
    zigbeeElement.appendChild(ValueElement);

    QDomText Datatext;
    QDomElement DataElement = doc.createElement("Data");
    QString data = InfraredData.m_data.toHex().toUpper();

    Datatext = doc.createTextNode(data);
    DataElement.appendChild(Datatext);
    zigbeeElement.appendChild(DataElement);


    QDomText Nametext;
    QDomElement NameElement = doc.createElement("Name");
    QString Name = InfraredData.m_name;

    Nametext = doc.createTextNode(Name);
    NameElement.appendChild(Nametext);
    zigbeeElement.appendChild(NameElement);


    root.appendChild(zigbeeElement);

    file.open(QFile::WriteOnly|QFile::Truncate); //先读进来，再重写，如果不用truncate就是在后面追加内容，就无效了
    QTextStream outStream(&file);
    doc.save(outStream,4); //缩进4格
    file.flush();
    file.close();
    parser();
    return true;
}

bool Zigbee3InfraredXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    return true;
}

QByteArray Zigbee3InfraredXml::generateData(Package package)
{
    QByteArray returnData;
    QByteArray packageData;
    returnData.clear();
    packageData.clear();
    int size=0;
    packageData.append(package.body());
    qDebug()<<"bodyfunciton="<<package.function();
    qDebug()<<"bodysize"<<packageData.size();
    if(package.function()==0xDC)
    {
        size=packageData.size()+1;
    }
//    else if(package.function()==0xD0)
//    {
//        size=packageData.size()+1;
//    }
    else
    {
        size=packageData.size();
    }


    returnData.append(0xaa);
    returnData.append(0xaa);
    returnData.append((char)0x00);
    if(package.function()==0xDC)
    {
      returnData.append(0x17);
    }
    else if(package.function()==0xD0)
    {
      returnData.append(0x16);
    }

    returnData.append(package.function());
    returnData.append((char)0x00);
    returnData.append(size);
    if(package.function()==0xDC)
    {
        returnData.append(0x01);
    }
    returnData.append(packageData);

    quint8 num=0;
    for(int i=0;i<returnData.size();i++)
    {
        num=num^returnData.at(i);
    }
    returnData.append(num);
    returnData.append(0x55);
    returnData.append(0x55);

    return returnData;


}

QByteArray Zigbee3InfraredXml::getInfraredID(quint16 addr)
{
    QByteArray data;
    data.clear();
    quint8 Hdata=(addr>>8)+1;
    quint8 Ldata=addr+1;
    for(int i=0;i<14;i++)
    {
    data.append(0x88);
    }
    data.append(Hdata);
    data.append(Ldata);
    return data;

}
